<template>
  <div class="list-box">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="downLoading"
        :finished="finished"
        :error.sync="error"
        finished-text="没有更多了"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <van-cell v-for="(item,index) in list" :key="index" clickable :title="item.title">
          <div slot="title" class="title van-multi-ellipsis--l2">{{ item.title }}</div>
          <div slot="label">
            <div v-if="item.cover.type===3" class="img-list">
              <van-image
                v-for="(img,i) in item.cover.images"
                :key="i"
                fit="cover"
                :src="img"
              />
            </div>
            <div class="sub-title">
              <span>{{ item.aut_name }}</span>
              <span>{{ item.comm_count }}评论</span>
              <span>{{ item.pubdate|toDay }}</span>
            </div>
          </div>
          <div v-if="item.cover.type===1" slot="default">
            <van-image
              class="one-img"
              fit="cover"
              :src="item.cover.images[0]"
            />
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import { getArticles } from '@/api/user'
export default {
  props: {
    queryObj: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      refreshing: false,
      downLoading: false,
      finished: false,
      error: false,
      list: [],
      pre_timestamp: ''
    }
  },
  methods: {
    async onRefresh() {
      try {
        const { data } = await getArticles({
          channel_id: this.queryObj.id,
          timestamp: this.pre_timestamp || Date.now(),
          with_top: 0
        })
        this.pre_timestamp = data.pre_timestamp
        // 下拉刷新将刷新的数据放到前面
        this.list = [...data.data.results, ...this.list]
        this.$toast('刷新成功')
      } catch (err) {
        console.log(err)
      } finally {
        this.refreshing = false
      }
    },
    async onLoad() {
      try {
        this.downLoading = true
        const { data } = await getArticles({
          channel_id: this.queryObj.id,
          timestamp: this.pre_timestamp || Date.now(),
          with_top: 0
        })
        console.log(data)
        this.pre_timestamp = data.pre_timestamp
        this.list = [...this.list, ...data.data.results]
        if (data.data.results.length === 0) this.finished = true
      } catch (err) {
        this.error = true
        console.log(err)
      } finally {
        this.downLoading = false
      }
    }
  }
}
</script>
<style lang="less" scoped>
.van-cell{
    padding:0;
    border-bottom: 1px solid #ddd;
    .van-cell__title{
        padding:20px;
    }
}
.list-box{
    padding-bottom: 100px;
    height: 79vh;
    overflow-y:auto;
    .title{
        font-size: 36px;
        font-weight: 600;
    }
    .img-list{
        display: flex;
        margin:15px 0;
        .van-image{
            flex: 1;
            height: 200px;
        }

    }
    .one-img{
        width:200px;
        height: 200px;
        padding:20px 20px 20px 0;
    }
    .sub-title{
        span{
            float: left;
            margin-right: 20px;
        }
    }
}
.van-cell__title{
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    justify-content: space-between;
}
.van-cell__value{
    flex: unset;
}
</style>
